//
//  YMHomeSegementView.m
//  YMMedical
//
//  Created by y s on 2023/7/19.
//

#import "YMHomeSegementView.h"

@interface YMHomeSegementView ()

@property (nonatomic, strong) UIView *vLine;

@end

@implementation YMHomeSegementView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    
}
- (void)setArrList:(NSArray *)arrList{
    _arrList = arrList;
    [self removeAllSubviews];
    if (arrList.count < 1) {
        return;
    }
    [self addSubview:self.vLine];
    UIButton *btnPre = nil;
    for (int i = 0; i < arrList.count; i++) {
        NSDictionary *dict = [SDataUtil dictOf:_arrList[i]];
        NSString *strTitle = [SDataUtil stringOf:dict[@"yzozfiekal0pbsij"]];
        UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
        [item setTitle:strTitle forState:UIControlStateNormal];
        [item setTitleColor:STheme.D3 forState:UIControlStateNormal];
        [item setTitleColor:STheme.D1 forState:UIControlStateSelected];
        item.titleLabel.font = [SFitUtil fitFont:16];
        [item addTarget:self action:@selector(segementClick:) forControlEvents:UIControlEventTouchUpInside];
        item.tag = 200 + i;
        [self addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            if (btnPre) {
                make.left.equalTo(btnPre.mas_right);
                make.width.mas_equalTo(btnPre);
            }else{
                make.left.mas_equalTo(0);
            }
            make.top.bottom.mas_equalTo(0);
            if (i == arrList.count - 1) {
                make.right.mas_equalTo(0);
            }
        }];
        btnPre = item;
    }
}

- (void)setIndexNow:(NSInteger)indexNow{
    _indexNow = indexNow;
    [self changeSelectBtnStatus:indexNow+200 isClick:NO];
}

- (void)segementClick:(UIButton *)sender{
    [self changeSelectBtnStatus:sender.tag isClick:YES];
}

- (void)changeSelectBtnStatus:(NSInteger)tag isClick:(BOOL)isClick{
    for (UIButton *item in self.subviews) {
        if ([item isKindOfClass:[UIButton class]]) {
            if (item.isSelected && item.tag == tag) {
                return;
            }else{
                if (item.tag == tag) {
                    item.selected = YES;
                    item.titleLabel.font = [SFitUtil fitBoldFont:16];
                    [self.vLine mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.centerX.equalTo(item);
                        make.size.mas_equalTo([SFitUtil fitSize:27 height:4]);
                        make.bottom.mas_equalTo(0);
                    }];
                }else{
                    item.selected = NO;
                    item.titleLabel.font = [SFitUtil fitFont:16];
                }
            }
        }
    }
        
    if (isClick && self.segementClickBlock) {
        self.segementClickBlock(tag-200);
    }
}


- (UIView *)vLine{
    if (_vLine == nil) {
        _vLine = [[UIView alloc] init];
        _vLine.backgroundColor = STheme.D12;
        _vLine.clipsToBounds = YES;
        _vLine.layer.cornerRadius = [SFitUtil fit:4]*0.5;
    }
    return _vLine;
}


@end
